using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using UmengSDK.Common;

namespace UmengSDK.Business
{
    internal class UpdateManager
    {
        public delegate void CheckCompletedHandler(out bool handled, Dictionary<string, object> dic);

        public UpdateManager.CheckCompletedHandler CheckCompletedEvent;

        public void CheckUpdate()
        {
            try
            {
                ThreadPool.QueueUserWorkItem(delegate (object s)
                {
                    DebugUtil.Log("Checking Update ...", "udebug----------->");
                    new NetTask(MessageType.CheckUpdate, null)
                    {
                        SendResponseCallback = new NetTask.ResponseCallback(this.OnUpdateCallback)
                    }.sendMessage();
                });
            }
            catch (Exception e)
            {
                DebugUtil.Log(e);
            }
        }

        private void OnUpdateCallback(string result)
        {
            if (result != null)
            {
                DebugUtil.Log(result, "udebug----------->");
                try
                {
                    Dictionary<string, object> dictionary = (Dictionary<string, object>)JSON.JsonDecode(result);
                    bool flag = false;
                    if (this.CheckCompletedEvent != null)
                    {
                        this.CheckCompletedEvent(out flag, dictionary);
                    }
                    if (!flag && dictionary.ContainsKey("update") && "yes".Equals(dictionary["update"].ToString().ToLower()))
                    {
                        string version = dictionary.ContainsKey("version") ? (dictionary["version"] as string) : string.Empty;
                        string description = dictionary.ContainsKey("update_log") ? (dictionary["update_log"] as string) : string.Empty;
                        string link = dictionary.ContainsKey("path") ? (dictionary["path"] as string) : string.Empty;
                        this.ShowUpdateDialog(version, description, link);
                    }
                    return;
                }
                catch (Exception e)
                {
                    DebugUtil.Log(e);
                    return;
                }
            }
            DebugUtil.Log("Fail to get Update info ...", "udebug----------->");
        }

        private void ShowUpdateDialog(string version, string description, string link)
        {
            StringBuilder uinfo = new StringBuilder();
            uinfo.Append("Latest version:").Append(version).Append("\n").Append(description);
            //Application.Current.Dispatcher.BeginInvoke(new Action(delegate
            //{
            //    MessageBoxResult messageBoxResult = MessageBox.Show(uinfo.ToString(), "New version found", MessageBoxButton.OK);
            //    if (messageBoxResult == MessageBoxResult.OK)
            //    {
            //        this.OpenMarket(link);
            //    }
            //}));
        }

        private void OpenMarket(string link)
        {
            if (string.IsNullOrEmpty(link))
            {
                return;
            }
            try
            {
                //WebBrowserTask webBrowserTask = new WebBrowserTask();
                //webBrowserTask.set_Uri(new Uri(link));
                //webBrowserTask.Show();
            }
            catch (Exception e)
            {
                DebugUtil.Log("Fail to open url : " + link, e);
            }
        }
    }
}
